<?php
include_once('common/header.php');
if($AppUI->id > 0) {
	$objCommon			= new Vfriend_Common();
	$objVfriendUser		= new Vfriend_User();
	$intUserId			= $AppUI->id;
	$intType 			= $objRequest->getParam('ptype', 0);
	if($intType == 1) {
		$strUsername		= $objRequest->getParam('pusername', '');
		$strMobile			= $objRequest->getParam('pmobile', '');
		$intEducation		= $objRequest->getParam('peducation', 0);
		$strEducationType	= $objRequest->getParam('peducation_type', '');
		$intOccupation		= $objRequest->getParam('poccupation', 0);
		$strOccupationType	= $objRequest->getParam('poccupation_type', '');
		$intPosition		= $objRequest->getParam('pposition', 0);
		$strPositionType	= $objRequest->getParam('pposition_type', '');
		$intMarital			= $objRequest->getParam('pmarital', 0);
		$strMaritalType		= $objRequest->getParam('pmarital_type', '');
		$intReligion		= $objRequest->getParam('preligion', 0);
		$strReligionype		= $objRequest->getParam('preligion_type', '');
		$strIntroduce		= $objRequest->getParam('pintroduce', '');	
		$intError			= -1;
		if(	$strUsername == '' || 
			$intEducation == '' || ($intEducation == -1 && $strEducationType == '') || 
			$intOccupation == '' || ($intOccupation == -1 && $strOccupationType == '') ||
			$intPosition == '' || ($intPosition == -1 && $strPositionType == '') ||
			$intMarital == '' || ($intMarital == -1 && $strPositionType == '') ||
			$intReligion == '' || ($intReligion == -1 && $strReligionype == '') ||
			$strIntroduce == '' || ($strIntroduce != '' && strlen($strIntroduce) < 10)
		)
			$intError = 2;
		if($intError == -1)
			$intError = $objVfriendUser->update($intUserId, $strUsername, $strMobile, $strIntroduce, $intType);
		if($intError == 0) {
			if($intEducation == -1) {
				$intEducation 	= $objCommon->addCategory($intUserId, 4, $strEducationType);
			}
			if($intOccupation == -1) { 
				$intOccupation	= $objCommon->addCategory($intUserId, 5, $strOccupationType);
			}
			if($intPosition == -1) { 
				$intPosition	= $objCommon->addCategory($intUserId, 6, $strPositionType);
			}
			if($intMarital == -1) { 
				$intMarital	= $objCommon->addCategory($intUserId, 2, $strMaritalType);
			}
			if($intReligion == -1) { 
				$intReligion	= $objCommon->addCategory($intUserId, 1, $strReligionype);
			}
			$objVfriendUser->addUpdateBasic($intUserId, $intReligion, $intOccupation, $intEducation, $intPosition, $intMarital);
			// clear cache
			$objCache->clear->deleteUserByEmail($AppUI->email);
			$objCache->clear->deleteUserByUsername($AppUI->username);
			Vfriend_Function::redirect(LINK_USER_PREFERENCE);	
			exit();
		}
		Vfriend_Function::redirect(LINK_USER_PERSON . '?err=' . $intError);
		exit();
	} elseif($intType == 2) {
		$arrParam 	= $objRequest->getParams();
		$arrMusic	= isset($arrParam['pmusic']) ? $arrParam['pmusic'] : null;
		$arrSport	= isset($arrParam['psport']) ? $arrParam['psport'] : null;
		$arrOther	= isset($arrParam['pother']) ? $arrParam['pother'] : null;
		if($arrMusic || $arrSport || $arrOther) {
			if($arrMusic) {
				$objVfriendUser->deleteUPreference($AppUI->id, QUESTION_ID_MUSIC);
				foreach($arrMusic as $value) {
					$objVfriendUser->addUPreference($AppUI->id, QUESTION_ID_MUSIC, $value);
				}
			}
			if($arrSport) {
				$objVfriendUser->deleteUPreference($AppUI->id, QUESTION_ID_SPORT);
				foreach($arrSport as $value) {
					$objVfriendUser->addUPreference($AppUI->id, QUESTION_ID_SPORT, $value);
				}
			}
			if($arrSport) {
				$objVfriendUser->deleteUPreference($AppUI->id, QUESTION_ID_OTHER);
				foreach($arrOther as $value) {
					$objVfriendUser->addUPreference($AppUI->id, QUESTION_ID_OTHER, $value);
				}
			}
		}
		else {
			$objVfriendUser->deleteUPreference($AppUI->id, QUESTION_ID_MUSIC);
			$objVfriendUser->deleteUPreference($AppUI->id, QUESTION_ID_SPORT);
			$objVfriendUser->deleteUPreference($AppUI->id, QUESTION_ID_OTHER);
			$objVfriendUser->updateAddinfo($AppUI->id);
		}
		Vfriend_Function::redirect(LINK_USER_EDUCATION_WORKING);
		exit();
	} elseif($intType == 8) {
		
		$strHighSchool 	= $objRequest->getParam('phschool','');
		$strHSCountry 	= $objRequest->getParam('phscountry','');
		$intHSProvince 	= $objRequest->getParam('phsprovince',null);
		$strHSAddr	 	= $objRequest->getParam('phsaddr',null);
		$strCollege 	= $objRequest->getParam('pcollege','');
		$strCCountry 	= $objRequest->getParam('pccountry','');
		$intCProvince 	= $objRequest->getParam('pcprovince',null);
		$strCAddr	 	= $objRequest->getParam('pcaddr',null);
		$strEmployer 	= $objRequest->getParam('pemployer','');
		$strECountry 	= $objRequest->getParam('pecountry','');
		$intEProvince 	= $objRequest->getParam('peprovince','');
		$strEAddr	 	= $objRequest->getParam('peaddr','');
		$intError		= 0;
		
		if($strHighSchool == '' && $strHSCountry == '' && ( $intHSProvince == '' || is_null($intHSProvince) )&& ( $strHSAddr == '' || is_null($strHSAddr) ) && 
			$strCollege == '' && $strCCountry == '' && ($intCProvince == '' || is_null($intCProvince)) && ( $strCAddr == '' || is_null($strCAddr) ) &&
			$strEmployer == '' && $strECountry == '' && ($intEProvince == '' || is_null($intEProvince) ) && ($strEAddr == '' || is_null($strEAddr)))
			$intError = 3;
		if($strHighSchool != '') {			
			if(strlen($strHighSchool) < 5)
				$intError = 4;
			elseif($strHSCountry == '')
				$intError = 5;
			elseif($intHSProvince == '' && is_null($strHSAddr))
				$intError = 6;
			elseif(is_null($intHSProvince) && $strHSAddr == '') 
				$intError = 7;
			elseif(is_null($intHSProvince) && $strHSAddr != '' && strlen($strHSAddr) < 5)
				$intError = 8;			
		}
		if($strCollege != '') {
			if(strlen($strCollege) < 5)
				$intError = 9;
			elseif($strCCountry == '')
				$intError = 10;
			elseif($intCProvince == '' && is_null($strCAddr))
				$intError = 11;
			elseif(is_null($intCProvince) && $strCAddr == '') 
				$intError = 12;
			elseif(is_null($intCProvince) && $strCAddr != '' && strlen($strCAddr) < 5)
				$intError = 13;
		}
		if($strEmployer != '') {
			if(strlen($strEmployer) < 5)
				$intError = 14;
			elseif($strECountry == '')
				$intError = 15;
			elseif($intEProvince == '' && is_null($strEAddr))
				$intError = 16;
			elseif(is_null($intEProvince) && $strEAddr == '') 
				$intError = 17;
			elseif(is_null($intEProvince) && $strEAddr != '' && strlen($strEAddr) < 5)
				$intError = 18;
		}	
		if($intError > 0)	{			
			$_SESSION['ework']	= $objRequest->getParams();
			Vfriend_Function::redirect(LINK_USER_EDUCATION_WORKING . '?err=' . $intError);
			exit();
		}
		else {	
			if($strEmployer != '') {
				$objVfriendUser->addUEWork($intUserId,$strEmployer,$strEAddr,$intEProvince,$strECountry,1);
			}
			if($strHighSchool != '') {
				$objVfriendUser->addUEWork($intUserId,$strHighSchool,$strHSAddr,$intHSProvince,$strHSCountry,2);			
			}
			if($strCollege != '') {
				$objVfriendUser->addUEWork($intUserId,$strCollege,$strCAddr,$intCProvince,$strCCountry,3);
			}
			Vfriend_Function::redirect(LINK_USER_MFRIEND_STANDARD);
		}
	}elseif($intType == 16) {		
		$arrParams			= $objRequest->getParams();
		$intLookGender		= isset($arrParams['plook_gender']) ? $arrParams['plook_gender'] : 0;
		$intLookAfrom		= isset($arrParams['plook_afrom']) ? $arrParams['plook_afrom'] : 0;
		$intLookAto			= isset($arrParams['plook_ato']) ? $arrParams['plook_ato'] : 0;
		$strLookCountry		= isset($arrParams['plook_country']) ? $arrParams['plook_country'] : '';
		$intLookProvince	= isset($arrParams['plook_province']) ? $arrParams['plook_province'] : 0;
		$arrLookEducation	= isset($arrParams['plook_education']) ? $arrParams['plook_education'] : array();
		$arrLookOccupation	= isset($arrParams['plook_occupation']) ? $arrParams['plook_occupation'] : array();
		$arrLookReligion	= isset($arrParams['plook_religion']) ? $arrParams['plook_religion'] : array();
		$strLookWish		= isset($arrParams['plook_wish']) ? $arrParams['plook_wish'] : '';
		$oFile				= $_FILES['pimage'];
		if(isset($oFile) && $oFile) {
			$arrImg = Globals::uploadAvatar($oFile);
			if($arrImg['error'] == "") {
				$objPhoto 	= new Photo();
				$arrResult	= $objPhoto->add($AppUI->id, $arrImg['path']);
				$intImageId	= isset($arrResult['imageid']) ? $arrResult['imageid'] : 0;
				$isUpdate	= isset($arrResult['is_update']) ? $arrResult['is_update'] : 0;
				if($isUpdate && $intImageId) {
					$objPhoto->unlinkImg($intImageId);
				}
			}
			else {
				Vfriend_Function::setError($arrImg['error']);
				Vfriend_Function::redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_VFRIEND);
			}
		}			
		if($arrLookEducation || $arrLookOccupation || $arrLookReligion) {
			if($arrLookEducation) {
				$objVfriendUser->deleteUPreference($AppUI->id, QUESTION_ID_LEDUCATION, 1);
				if($arrLookEducation[0] != 0) {
					foreach($arrLookEducation as $value) {
						$objVfriendUser->addUPreference($AppUI->id, QUESTION_ID_LEDUCATION, $value, 1);
					}
				}
			}
			if($arrLookOccupation) {
				$objVfriendUser->deleteUPreference($AppUI->id, QUESTION_ID_LOCCUPATION, 1);
				if($arrLookOccupation[0] != 0) {
					foreach($arrLookOccupation as $value) {
						$objVfriendUser->addUPreference($AppUI->id, QUESTION_ID_LOCCUPATION, $value, 1);
					}
				}
			}
			if($arrLookReligion) {
				$objVfriendUser->deleteUPreference($AppUI->id, QUESTION_ID_LRELIGION, 1);
				if($arrLookReligion[0] != 0) {
					foreach($arrLookReligion as $value) {
						$objVfriendUser->addUPreference($AppUI->id, QUESTION_ID_LRELIGION, $value, 1);
					}
				}
			}
		}
		else {
			$objVfriendUser->deleteUPreference($AppUI->id, QUESTION_ID_LEDUCATION, 1);
			$objVfriendUser->deleteUPreference($AppUI->id, QUESTION_ID_LOCCUPATION, 1);
			$objVfriendUser->deleteUPreference($AppUI->id, QUESTION_ID_LRELIGION, 1);			
		}
		
		$objVfriendUser->updateLook($AppUI->id, $intLookGender, $strLookCountry, $intLookProvince, $intLookAfrom, $intLookAto, $strLookWish);
		Vfriend_Function::redirect(HOST_VFRIEND);
	}
}else {
	Vfriend_Function::redirect(HOST_VFRIEND);
	exit();
}
?>